package com.ys.controller;

import com.github.pagehelper.PageHelper;
import com.ys.mapper.ChukubiaoMapper;
import com.ys.mapper.DrugsMapper;
import com.ys.mapper.OrderMapper;
import com.ys.mapper.OrderdrugcommodityMapper;
import com.ys.pojo.*;
import com.ys.service.OrderService;
import com.ys.service.OrderdrugcommodityService;
import com.ys.vo.PageVo;
import com.ys.vo.ResultVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.servlet.ShiroFilter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;

@Controller
public class XiaoShouController {

    @Resource
    private OrderService orderService;

    @Resource
    private OrderdrugcommodityService orderdrugcommodityService;
    @Resource
    private OrderdrugcommodityMapper orderdrugcommodityMapper;

    @Resource
    private OrderMapper orderMapper;


    @RequestMapping("/order")
    @ResponseBody
    public ResultVo order(String gouMaiRen,String gouMaiGongSi,String types){
        ResultVo resultVo = new ResultVo();
        Order order = new Order();
        int s  =(int)((Math.random()*9+1)*100000);
        order.setOrideridid(String.valueOf(s));//毫秒数
        order.setGoumairen(gouMaiRen);//购买人
        order.setGoumaigongsi(gouMaiGongSi);//购买公司
        order.setGoumaishijian(LocalDate.now());//购买时间
        order.setTypes(types);
        orderService.addsa(order);
        resultVo.setMsg(String.valueOf(order.getId()));
        Chukubiao chukubiao = new Chukubiao();
        chukubiao.setDangqiantiem(LocalDate.now());
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        Administrators administrators = (Administrators)session.getAttribute("objs");
        chukubiao.setDangqianuser(administrators.getId());
        chukubiao.setChukuorderids(order.getId());
        chukubiaoMapper.add(chukubiao);
        return resultVo;
    }

    @RequestMapping("/xiaoshouchaxun")
    @ResponseBody
    public ResultVo xiaoshouchaxun(String id,String name1,String pageNo,String pageSize,String name2
            ,String name3,String name4){
        PageHelper.startPage(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
        ResultVo resultVo = new ResultVo();
        resultVo.setObj(new PageVo(drugsMapper.xiaoshouchaxun(id,name1,name2,name3,name4,String.valueOf(1))));
        return resultVo;
    }

    @Resource
    private ChukubiaoMapper chukubiaoMapper;
    @Resource
    private DrugsMapper drugsMapper;

    @RequestMapping("/orderdingdan")
    @ResponseBody
    public ResultVo orderdingdan(@RequestBody Drugs drugs){
        ResultVo resultVo = new ResultVo();
        Orderdrugcommodity orderdrugcommodity = new Orderdrugcommodity();
        orderdrugcommodity.setOrderids(drugs.getOrderid());//订单编号
        orderdrugcommodity.setDrugsid(drugs.getDrugsid());//药品编号
        orderdrugcommodity.setNumber(drugs.getPanNum());//药品数量
        orderdrugcommodity.setPrice(drugs.getPrices());//药品单价
        orderdrugcommodityService.addsinsert(orderdrugcommodity);
        drugsMapper.updatekucun(drugs.getPanNum(),String.valueOf(drugs.getDrugsid()));
        orderMapper.zongjia(drugs.getOrderid());
        return resultVo;
    }

    @RequestMapping(value = "/Tuihuoorderdingdan", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ResultVo Tuihuoorderdingdan(@RequestBody Drugs drugs){
        ResultVo resultVo = new ResultVo();
        Orderdrugcommodity orderdrugcommodity = new Orderdrugcommodity();
        orderdrugcommodity.setOrderids(drugs.getOrderid());//订单编号
        orderdrugcommodity.setDrugsid(drugs.getDrugsid());//药品编号
        orderdrugcommodity.setNumber(drugs.getNumber());//药品数量
        orderdrugcommodity.setPrice(drugs.getPrices());//药品单价
        orderdrugcommodityService.addsinsert(orderdrugcommodity);
        drugsMapper.updatekucun(drugs.getPanNum(),String.valueOf(drugs.getDrugsid()));
        orderMapper.zongjia(drugs.getOrderid());
        orderdrugcommodityMapper.gaibiansNumber(String.valueOf(drugs.getNumber()),String.valueOf(drugs.getDrugsid()));
        return resultVo;
    }


    @RequestMapping(value = "/Tuihuoorder", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ResultVo Tuihuoorder(String gouMaiRen,String gouMaiGongSi,String tuihuoxinxi){
        ResultVo resultVo = new ResultVo();
        Order order = new Order();
        int s  =(int)((Math.random()*9+1)*100000);
        order.setOrideridid(String.valueOf(s));//毫秒数
        order.setGoumairen(gouMaiRen);//购买人
        order.setGoumaigongsi(gouMaiGongSi);//购买公司
        order.setGoumaishijian(LocalDate.now());//购买时间
        order.setTypes("退货");
        order.setConcentmiao(tuihuoxinxi);
        orderService.addsa(order);
        resultVo.setMsg(String.valueOf(order.getId()));
        Chukubiao chukubiao = new Chukubiao();
        chukubiao.setDangqiantiem(LocalDate.now());
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        Administrators administrators = (Administrators)session.getAttribute("objs");
        chukubiao.setDangqianuser(administrators.getId());
        chukubiao.setChukuorderids(order.getId());
        chukubiaoMapper.add(chukubiao);
        return resultVo;
    }

//    @RequestMapping("/Tuihuorderdingdan")
//    @ResponseBody
//    public ResultVo Tuihuorderdingdan(@RequestBody Drugs drugs){
//        ResultVo resultVo = new ResultVo();
//        System.out.println(drugs.getNumber()+"======数量");
//        Orderdrugcommodity orderdrugcommodity = new Orderdrugcommodity();
//        orderdrugcommodity.setOrderids(drugs.getOrderid());//订单编号
//        orderdrugcommodity.setDrugsid(drugs.getDrugsid());//药品编号
//        orderdrugcommodity.setNumber(drugs.getJianumber());//药品数量
//        orderdrugcommodity.setPrice(drugs.getPrice());//药品单价
//        System.out.println(drugs.getOrderid()+"=========订单的id");
//        orderdrugcommodityService.addsinsert(orderdrugcommodity);
//        drugsMapper.updatekucun(drugs.getNumber(),String.valueOf(drugs.getDrugsid()));
//        orderMapper.zongjia(drugs.getOrderid());
//        return resultVo;
//    }



}
